/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  					        abstract_resource_cache.cpp

	$Header: /game/abstract_resource_cache. $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "abstract_resource_cache.h"

#include "resource_dir.h"
#include "debug_message.h"


int  t_abstract_cache_base::get_load_cost()
{
	return 0;
}

// -------------------------------------------------------------------
// cached variable
// -------------------------------------------------------------------
t_abstract_resource_cache_base::t_abstract_resource_cache_base( std::string const & name )
							  : m_valid( true ), m_name( name )
{
	m_size = 0;
}

std::string t_abstract_resource_cache_base::get_name()
{
	find_resource(); // force the appropriate name mangling, and update to the actual model name
	return m_name;
}

// -------------------------------------------------------------------
// cached variable
// -------------------------------------------------------------------
bool t_abstract_resource_cache_base::find_resource()
{
	// fail if we have tried and failed to create the item before.
	if (!m_valid)
		return false;
	
	// do we know where the data is?
	if (m_file == 0)
	{	// if not, find it.
		std::string name = get_prefix();

		name += '.';
		name += m_name;
		name += ".h4d";
		m_name = name;
		t_resource_entry const* entry = ::find_resource( name );
		
		// if we couldn't find it, return false.
		if (entry == 0)
		{
			m_valid = false;
			return false;
		}
		// record the data's location.
		m_offset = entry->offset;
		m_file = entry->file;
		m_size = entry->size;
		m_name = entry->name;
	}
	return true;
}

// -------------------------------------------------------------------
// cached variable
// -------------------------------------------------------------------
int t_abstract_resource_cache_base::get_size() 
{
	find_resource();
	return m_size;
}

// -------------------------------------------------------------------
// cached variable
// -------------------------------------------------------------------
bool t_abstract_resource_cache_base::create( t_progress_handler* handler )
{
	// fail if we have tried and failed to create the item before.
	if (!find_resource())
		return false;

	// get a stream from the file.
	t_streambuf_ptr stream = m_file->get_stream( m_offset );

	// fail if we didn't get a stream.
	if (stream == 0)
	{
		m_valid = false;
		return false;
	}

	// create a new variable and read it from the stream.
	if (!read( *stream, handler ))
	{   // if that failed, delete the variable and exit.
		DEBUG_MESSAGE(m_name.c_str());
		release_memory();
		m_valid = false;
		return false;
	}
	check_memory();
	return true;
}

